import menu from "@/api/menu"
const state = {
  menus: [],
  bookmark:{},
  selectedMenu:{}
};

const getters = {
  menus: function(state) {
    return state.menus;
  },
  bookmark: function(state) {
    return state.bookmark;
  }
};

const actions = {
  actionMenus({ commit, state }) {
    return new Promise((resolve, reject) => {
      if(state.menus && state.menus.length>=1 ){
        resolve();
        return;
      }
      // 获取菜单
      menu.getMenus().then(data => {
        sessionStorage.setItem("currentMenus", JSON.stringify(data));
        commit("mutationMenus", data);
        resolve(data);
      })

    });
  },
  actionMenusEmpty({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit("mutationMenus", []);
    });
  },
};

const mutations = {
  mutationMenus(state, menus) {
    state.menus = menus;
  },
  setSelectedMenu(state,currentMenu){
    state.selectedMenu = currentMenu
  }
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};
